<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Alumni extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('flexigrid');
        $this->load->library('flexigrid');
        $this->load->library('session');
        $this->load->library('form_validation');
        $this->load->model('peserta_lulus_model');
        $this->load->model('peserta_model');
        $this->load->model('batch_model');
        $this->load->model('alumni_model');
        $this->load->model('kota_model');
		$this->cek_login();
    }
	
	
	function cek_login(){
		$id_hak = $this->session->userdata('id_hak');
		if($id_hak < 1 || $id_hak > 3 || $id_hak == ''){
			redirect ('login');
		}
	}
	
	
    function index() {
        $colModel['no'] = array('No', 30, TRUE, 'center', 0);
        $colModel['Id_Alumni'] = array('No Alumni', 70, TRUE, 'center', 1);
        $colModel['Nama_Alumni'] = array('Nama Alumni', 120, TRUE, 'center', 1);
        $colModel['Jenis_Kelamin'] = array('Jenis Kelamin', 75, TRUE, 'center', 0);
        $colModel['Tempat_Lahir'] = array('Tempat Lahir', 75, TRUE, 'center', 1);
        $colModel['No_HP'] = array('No HP', 75, TRUE, 'center', 1);
        $colModel['Pekerjaan'] = array('Pekerjaan', 100, TRUE, 'center', 1);
        $colModel['Jenis_Usaha'] = array('Jenis Usaha', 120, TRUE, 'center', 1);
        $colModel['Omset'] = array('Omset', 75, TRUE, 'left', 0);
        $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);


        $gridParams = array(
            'width' => 'auto',
            'height' => 300,
            'rp' => 10,
            'rpOptions' => '[5,10,15,20,25,40]',
            'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Alumni',
            'showTableToggleBtn' => true
        );

        //$buttons[] = array('Tambah','add','test');
        //$buttons[] = array('separator');

        $buttons[] = array('Hapus', 'delete', 'spt_js');

        // mengambil data dari file controler ajax pada method grid_berkas
        $grid_js = build_grid_js('flex1', site_url("alumni/alumni/grid_alumni"), $colModel, 'Nama_Alumni', 'asc', $gridParams, $buttons);


        //additional JS
        $data['added_js'] =
                "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='" . base_url() . "index.php/peserta/peserta/form_peserta'; 
			}
			
			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' data alumni?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/alumni/alumni/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					} 
				}
				
		} 
		</script>
		";


        $data_grid['js_grid'] = $grid_js;


        //remdering view
        $data['title'] = 'Daftar Peserta Lulus';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'alumni/grid_alumni', $data_grid);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    function grid_alumni() {
        $valid_fields = array('Id_Alumni','Nama_Alumni', 'Jenis_Kelamin', 'Tempat_Lahir', 'No_HP', 'Pekerjaan', 'Jenis_Usaha', 'Omset', 'Status');
        $this->flexigrid->validate_post('Nama_Alumni', 'asc', $valid_fields);
        $records = $this->alumni_model->get_data_alumni();
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
            $record_items[] = array(
                $row->ID_ALUMNI,
                $no = $no + 1,
                $row->ID_ALUMNI,
                $row->NAMA_ALUMNI,
                $row->JENIS_KELAMIN,
                $row->TEMPAT_LAHIR,
                $row->NO_HP,
                $row->PEKERJAAN,
                $row->JENIS_USAHA,
                'Rp. ' . $row->OMSET . ',-',
                '<a href=\'' . base_url() . 'index.php/alumni/alumni/edit/' . $row->ID_ALUMNI . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/edit.png\'></a> '
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

    public function detail($id) {
        $record_peserta = $this->peserta_model->selectone($id);
        foreach ($record_peserta->result() as $peserta) {
            $data['id_peserta'] = $peserta->ID_PESERTA;
            $data['nama_peserta'] = $peserta->NAMA_PESERTA;
            $data['nama_panggilan'] = $peserta->NAMA_PANGGILAN;
            $data['jenis_kelamin'] = $peserta->JENIS_KELAMIN;
            $data['tempat_lahir'] = $peserta->TEMPAT_LAHIR;
            $data['tanggal'] = $peserta->TANGGAL;
            $data['bulan'] = $peserta->BULAN;
            $data['tahun'] = $peserta->TAHUN;
            $data['nama_usaha'] = $peserta->NAMA_USAHA;
            $data['jenis_usaha'] = $peserta->JENIS_USAHA;
            $data['omset'] = $peserta->OMSET;
            $data['pekerjaan'] = $peserta->PEKERJAAN;
            $data['no_hp'] = $peserta->NO_HP;
            $data['no_darurat'] = $peserta->NO_DARURAT;
            $data['hub_darurat'] = $peserta->HUB_DARURAT;
            $data['email'] = $peserta->EMAIL;
            $data['twitter'] = $peserta->TWITTER;
            $data['ukuran_baju'] = $peserta->UKURAN_BAJU;
            $data['alamat'] = $peserta->ALAMAT;
            $data['kode_pos'] = $peserta->KODE_POS;
            $data['kota'] = $peserta->KOTA;
            $data['referensi'] = $peserta->REFERENSI;
            $data['no_transfer'] = $peserta->NO_TRANSFER;
            $data['tanggal_pembayaran'] = $peserta->TANGGAL_PEMBAYARAN;
            $data['promo'] = $peserta->PROMO;
            $data['informasi'] = $peserta->INFORMASI;
            $data['id_batch'] = $peserta->ID_BATCH;
        }

        //get data batch
        $id = 1;
        $data['batch'] = $this->batch_model->get_batch($id);


        $data['status'] = 'preview';
        $data['title'] = 'Tambah Peserta';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'alumni/form_alumni', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    public function edit($id) {
        $data['status'] = 'edit';
        $record_peserta = $this->alumni_model->selectone($id);
        foreach ($record_peserta->result() as $peserta) {
            $data['id_alumni'] = $peserta->ID_ALUMNI;
            $data['nama_alumni'] = $peserta->NAMA_ALUMNI;
            $data['nama_panggilan'] = $peserta->NAMA_PANGGILAN;
            $data['jenis_kelamin'] = $peserta->JENIS_KELAMIN;
            $data['tempat_lahir'] = $peserta->TEMPAT_LAHIR;
            $data['tanggal'] = $peserta->TANGGAL;
            $data['bulan'] = $peserta->BULAN;
            $data['tahun'] = $peserta->TAHUN;
            $data['nama_usaha'] = $peserta->NAMA_USAHA;
            $data['jenis_usaha'] = $peserta->JENIS_USAHA;
            $data['omset'] = $peserta->OMSET;
            $data['pekerjaan'] = $peserta->PEKERJAAN;
            $data['no_hp'] = $peserta->NO_HP;
            $data['email'] = $peserta->EMAIL;
            $data['twitter'] = $peserta->TWITTER;
            $data['alamat'] = $peserta->ALAMAT;
            $data['kode_pos'] = $peserta->KODE_POS;
            $data['id_kota'] = $peserta->ID_KOTA;
            $data['back_link'] = '' . base_url() . 'index.php/alumni/alumni';
            $data['id_batch'] = $peserta->ID_BATCH;
        }

        $id = 1;
        $data['batch'] = $this->batch_model->get_batch($this->session->userdata('id_lisensi'));
        $data['kota'] = $this->kota_model->get_all_kota();
        $data['title'] = 'Edit Data Alumni';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'alumni/form_alumni', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    public function preview_by_user() {
        $colModel['no'] = array('No', 30, TRUE, 'center', 0);
        $colModel['Nama_Alumni'] = array('Nama Alumni', 120, TRUE, 'center', 1);
        $colModel['Jenis_Kelamin'] = array('Jenis Kelamin', 75, TRUE, 'center', 0);
        $colModel['Tempat_Lahir'] = array('Tempat Lahir', 75, TRUE, 'center', 1);
        $colModel['Email'] = array('Email', 150, TRUE, 'center', 1);
        $colModel['Pekerjaan'] = array('Pekerjaan', 150, TRUE, 'center', 1);
        $colModel['Jenis_Usaha'] = array('Jenis Usaha', 120, TRUE, 'center', 1);
        $colModel['Omset'] = array('Omset', 75, TRUE, 'left', 0);


        $gridParams = array(
            'width' => 'auto',
            'height' => 300,
            'rp' => 10,
            'rpOptions' => '[5,10,15,20,25,40]',
            'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Alumni',
            'showTableToggleBtn' => true
        );
        
        $buttons[] = array('Perbarui Data Anda', 'add', 'spt_js');
        $buttons[] = array('separator');

        // mengambil data dari file controler ajax pada method grid_berkas
        $grid_js = build_grid_js('flex1', site_url("alumni/alumni/grid_preview_by_user"), $colModel, 'Nama_Alumni', 'asc', $gridParams, $buttons);


        //additional JS
        
        $data['added_js'] =
                "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Perbarui Data Anda')
			{
				location.href='" . base_url() . "index.php/alumni/alumni/edit_by_user'; 
			}
			
			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' data alumni?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/alumni/alumni/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					} 
				}
				
		} 
		</script>
		";


        $data_grid['js_grid'] = $grid_js;


        //remdering view for user alumni

        $data['title'] = 'Daftar Peserta Lulus';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'alumni/grid_alumni', $data_grid);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    function grid_preview_by_user() {
        $valid_fields = array('Nama_Alumni', 'Jenis_Kelamin', 'Tempat_Lahir', 'No_HP', 'Pekerjaan', 'Jenis_Usaha', 'Omset', 'Status');
        $this->flexigrid->validate_post('Nama_Alumni', 'asc', $valid_fields);
        $records = $this->alumni_model->get_data_alumni();
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
            $record_items[] = array(
                $row->ID_ALUMNI,
                $no = $no + 1,
                $row->NAMA_ALUMNI,
                $row->JENIS_KELAMIN,
                $row->TEMPAT_LAHIR,
                $row->EMAIL,
                $row->PEKERJAAN,
                $row->JENIS_USAHA,
                'Rp. ' . $row->OMSET . ',-'
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

    public function edit_by_user() {
        $id = $this->session->userdata('id_alumni');
        $data['status'] = 'edit';
        $record_peserta = $this->alumni_model->selectone($id);
        foreach ($record_peserta->result() as $peserta) {
            $data['id_alumni'] = $peserta->ID_ALUMNI;
            $data['nama_alumni'] = $peserta->NAMA_ALUMNI;
            $data['nama_panggilan'] = $peserta->NAMA_PANGGILAN;
            $data['jenis_kelamin'] = $peserta->JENIS_KELAMIN;
            $data['tempat_lahir'] = $peserta->TEMPAT_LAHIR;
            $data['tanggal'] = $peserta->TANGGAL;
            $data['bulan'] = $peserta->BULAN;
            $data['tahun'] = $peserta->TAHUN;
            $data['nama_usaha'] = $peserta->NAMA_USAHA;
            $data['jenis_usaha'] = $peserta->JENIS_USAHA;
            $data['omset'] = $peserta->OMSET;
            $data['pekerjaan'] = $peserta->PEKERJAAN;
            $data['no_hp'] = $peserta->NO_HP;
            $data['email'] = $peserta->EMAIL;
            $data['twitter'] = $peserta->TWITTER;
            $data['alamat'] = $peserta->ALAMAT;
            $data['kode_pos'] = $peserta->KODE_POS;
            $data['id_kota'] = $peserta->ID_KOTA;
            $data['back_link'] = '' . base_url() . 'index.php/alumni/alumni/preview_by_user';
            $data['id_batch'] = $peserta->ID_BATCH;
        }

        $id = 1;
        $data['batch'] = $this->batch_model->get_batch($this->session->userdata('id_lisensi'));
        $data['kota'] = $this->kota_model->get_all_kota();
        $data['title'] = 'Edit Data Alumni';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'alumni/form_alumni_user', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    //update peserta
    public function update_alumni($id) {
        if ($this->cek_validasi() == FALSE) {
            if ($this->session->userdata('id_hak') == 3) {
                $this->edit_by_user();
            } else {
                $this->edit($id);
            }
        } else {
            $tahun = $this->input->post('thn');
            $bulan = $this->input->post('bln');
            $tanggal = $this->input->post('tgl');
            $tanggal_lahir = '' . $tahun . '-' . $bulan . '-' . $tanggal . '';
            $data = array(
                // 'NAMA_ALUMNI' => $this->input->post('nama_alumni'),
                // 'NAMA_PANGGILAN' => $this->input->post('nama_panggilan'),
                // 'JENIS_KELAMIN' => $this->input->post('jenis_kelamin'),
                // 'TEMPAT_LAHIR' => $this->input->post('tempat_lahir'),
                // 'TANGGAL_LAHIR' => $tanggal_lahir,
                'NAMA_USAHA' => $this->input->post('nama_usaha'),
                'JENIS_USAHA' => $this->input->post('jenis_usaha'),
                'OMSET' => $this->input->post('omset'),
                'PEKERJAAN' => $this->input->post('pekerjaan'),
                'NO_HP' => $this->input->post('no_hp'),
                //'EMAIL' => $this->input->post('email'),
                'TWITTER' => $this->input->post('twitter'),
                'KODE_POS' => $this->input->post('kode_pos'),
                'ALAMAT' => $this->input->post('alamat'),
                'ID_KOTA' => $this->input->post('id_kota')
                //'ID_BATCH' => $this->input->post('batch')
            );
            $this->alumni_model->update_alumni($id, $data);
            if ($this->session->userdata('id_hak') == 3) {
                redirect('alumni/alumni/preview_by_user');
            } else {
                redirect('alumni/alumni');
            }
        }
    }

    function cek_validasi() {
        // Setting Rules
        // $this->form_validation->set_rules('nama_alumni', 'Nama Lengkap', 'required');
        // $this->form_validation->set_rules('nama_panggilan', 'Nama Panggilan', 'required');
        // $this->form_validation->set_rules('jenis_kelamin', 'Jenis Kelamin', 'required|callback_cek_dropdown');
        $this->form_validation->set_rules('alamat', 'Alamat Rumah', 'required');
        $this->form_validation->set_rules('kode_pos', 'Kode Pos', 'required|numeric');
        $this->form_validation->set_rules('id_kota', 'Kota', 'required|callback_cek_dropdown');
        $this->form_validation->set_rules('tempat_lahir', 'Tempat Lahir', 'required');
        //$this->form_validation->set_rules('thn', 'Tanggal Lahir', 'required');
        $this->form_validation->set_rules('no_hp', 'No Telepon/Handphone', 'required|numeric');
        //$this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('pekerjaan', 'Pekerjaan', 'required|callback_cek_dropdown');
        //$this->form_validation->set_rules('batch', 'Angkatan/Batch', 'required|callback_cek_dropdown');

        //Setting Error Message
        $this->form_validation->set_message('required', 'Kolom %s harus diisi.');
        $this->form_validation->set_message('numeric', 'Kolom %s harus diisi dengan karakter angka saja, desimal menggunakan titik (.)');
        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
        return $this->form_validation->run();
    }

    //cek_dropdown
    function cek_dropdown($value) {
        if ($value === '0') {
            $this->form_validation->set_message('cek_dropdown', 'Kolom %s harus diisi.');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function delete($id) {
        $spt_ids_post_array = split(",", $this->input->post('items'));
        //$this->load->model('pengolahan/data_model');
        //$msg='akun berhasil dihapus';
        foreach ($spt_ids_post_array as $index => $id) {
            if (isset($id) && $id != '') {
                $this->alumni_model->delete($id);
            }//end if
        }//end foreach
        //$this->peserta_model->delete($id);
        //redirect('peserta/peserta');
    }

}

/* End of file pegawai.php */
/* Location: ./application/controllers/pegawai.php */
